<?php
declare (strict_types=1);

namespace app\api\service\boss;

use app\api\service\boss\MBaseService;
use app\common\library\helper;
use app\api\model\boss\MRetailOrderV2;
use app\api\model\boss\MCurveV2;

use Mall\Framework\Core\ResultWrapper;

class MIndexV2 extends MBaseService
{
    private $objMRetailOrderV2;

    public function __construct()
    {
        $this->objMRetailOrderV2 = new MRetailOrderV2();
    }

    // 支付类型
    static function getPayNameByType($payType)
    {
        switch ($payType) {
            case 0:
                $payName = '合计营收';
                break;
            case 1:
                $payName = '现金营收';
                break;
            case 2:
                $payName = '三券营收';
                break;
            case 3:
                $payName = '赠品营收';
                break;
            case 4:
                $payName = '充值营收';
                break;
        }

        return $payName;
    }


    // 营收概况
    public function index($selectParams)
    {
        $returnData = [
            'money'       => [
                'name'                    => self::getPayNameByType($selectParams['payType']),
                'money'                   => 0,
                'moneyChainRelativeRatio' => '0',
                'moneyPositive'           => 0,
            ],
            'profitMoney' => [
                'name'                          => '毛利金额',
                'profitMoney'                   => 0,
                'profitMoneyChainRelativeRatio' => '0',
                'profitMoneyPositive'           => 0,
            ],
            'grossProfit' => [
                'name'                          => '毛利率',
                'grossProfit'                   => '0%',
                'grossProfitChainRelativeRatio' => '0',
                'grossProfitPositive'           => 0,
            ],
            'cost'        => [
                'name'                   => '计划成本',
                'cost'                   => 0,
                'costChainRelativeRatio' => '0',
                'costPositive'           => 0,
            ],
            'orderNum'    => [
                'name'                       => '客单数',
                'orderNum'                   => 0,
                'orderNumChainRelativeRatio' => '0',
                'orderNumPositive'           => 0,
            ],
            'saleNum'     => [
                'name'                      => '销售份数',
                'saleNum'                   => 0,
                'saleNumChainRelativeRatio' => '0',
                'saleNumPositive'           => 0,
            ],
        ];

        if ($selectParams['bangdouStoreIds'][0] == '6666'){
            return $returnData;
        }
        // 目前没有赠品支付方式直接返回默认数据
        if ($selectParams['payType'] == 3) {
            return $returnData;
        }

        $incomeData = $this->objMRetailOrderV2->income($selectParams);
        if (empty($incomeData)) {
            $this->error = '数据异常，查询失败';
            return false;
        }
        $money = 0;
        switch ($selectParams['incomeType']) {
            case 1: // 标价
                $money = $incomeData[0]['total'];
                break;
            case 2: // 业绩
                $money = $incomeData[0]['discount_price'];
                break;
            case 3: // 实际
                $money = $incomeData[0]['realIncome'];
                break;
        }
        $returnData['money']['money']             = $money ?: 0;
        $returnData['profitMoney']['profitMoney'] = helper::bcsub($money, $incomeData[0]['cost_money'], 2); // 动态计算毛利
        $returnData['grossProfit']['grossProfit'] = 0.00;

        if ($money > 0) {
            $returnData['grossProfit']['grossProfit'] = helper::bcmul(helper::bcdiv($returnData['profitMoney']['profitMoney'], $money, 2), 100, 2);
        }
        $returnData['cost']['cost']         = $incomeData[0]['cost_money'] ?: 0;
        $returnData['orderNum']['orderNum'] = $incomeData[0]['orderNum'] ?: 0;
        $returnData['saleNum']['saleNum']   = $incomeData[0]['goodsNum'] ?: 0;

        // 计算环比开始结束时间,并切换统计时间
        $selectParams              = parent::getChainRelativeTime($selectParams);
        $selectParams['startTime'] = $selectParams['chainRelativeStartTime'];
        $selectParams['endTime']   = $selectParams['chainRelativeEndTime'];

        $incomeDataChainRelative = $this->objMRetailOrderV2->income($selectParams, $selectParams['payType']);
        if (empty($incomeDataChainRelative)) {
            $this->error = '数据异常，查询失败';
            return false;
        }

        switch ($selectParams['incomeType']) {
            case 1: // 标价
                $money = $incomeDataChainRelative[0]['total'];
                break;
            case 2: // 业绩
                $money = $incomeDataChainRelative[0]['discount_price'];
                break;
            case 3: // 实际
                $money = $incomeDataChainRelative[0]['realIncome'];
                break;
        }

        $returnData['money']['moneyChainRelative']      = $money ?: 0;
        $returnData['money']['moneyChainRelativeRatio'] = self::chainRelativeRatio($returnData['money']['money'], $returnData['money']['moneyChainRelative']);
        $returnData['money']['moneyPositive']           = ($returnData['money']['moneyChainRelativeRatio'] > 0) ? 1 : 0;

        $returnData['profitMoney']['profitMoneyChainRelative']      = helper::bcsub($money, $incomeDataChainRelative[0]['cost_money'], 2);
        $returnData['profitMoney']['profitMoneyChainRelativeRatio'] = self::chainRelativeRatio($returnData['profitMoney']['profitMoney'], $returnData['profitMoney']['profitMoneyChainRelative']);
        $returnData['profitMoney']['profitMoneyPositive']           = ($returnData['profitMoney']['profitMoneyChainRelativeRatio'] > 0) ? 1 : 0;

        $returnData['grossProfit']['grossProfitChainRelative'] = 0.00;
        if ($returnData['money']['moneyChainRelative']>0) {
            $returnData['grossProfit']['grossProfitChainRelative'] = helper::bcmul(helper::bcdiv($returnData['profitMoney']['profitMoneyChainRelative'], $returnData['money']['moneyChainRelative'], 2), 100, 2);
        }

        $returnData['grossProfit']['grossProfitChainRelativeRatio'] = self::chainRelativeRatio($returnData['grossProfit']['grossProfit'], $returnData['grossProfit']['grossProfitChainRelative']);
        $returnData['grossProfit']['grossProfitPositive']           = ($returnData['grossProfit']['grossProfitChainRelativeRatio'] > 0) ? 1 : 0;

        $returnData['cost']['costChainRelative']      = $incomeDataChainRelative[0]['cost_money'] ?: 0;
        $returnData['cost']['costChainRelativeRatio'] = self::chainRelativeRatio($returnData['cost']['cost'], $returnData['cost']['costChainRelative']);
        $returnData['cost']['costPositive']           = ($returnData['cost']['costChainRelativeRatio'] > 0) ? 1 : 0;

        $returnData['orderNum']['orderNumChainRelative']      = $incomeDataChainRelative[0]['orderNum'] ?: 0;
        $returnData['orderNum']['orderNumChainRelativeRatio'] = self::chainRelativeRatio($returnData['orderNum']['orderNum'], $returnData['orderNum']['orderNumChainRelative']);
        $returnData['orderNum']['orderNumPositive']           = ($returnData['orderNum']['orderNumChainRelativeRatio'] > 0) ? 1 : 0;

        $returnData['saleNum']['saleNumChainRelative']      = $incomeDataChainRelative[0]['goodsNum'] ?: 0;
        $returnData['saleNum']['saleNumChainRelativeRatio'] = self::chainRelativeRatio($returnData['saleNum']['saleNum'], $returnData['saleNum']['saleNumChainRelative']);
        $returnData['saleNum']['saleNumPositive']           = ($returnData['saleNum']['saleNumChainRelativeRatio'] > 0) ? 1 : 0;
        return $returnData;
    }

    /**
     * 公共计算环比函数
     * $money  正常金额
     * $moneyChainRelative 环比金额
     */
    static function chainRelativeRatio($money, $moneyChainRelative)
    {
        $chainRelativeRatio = 0;
        if ($moneyChainRelative != 0) {
            $chainRelativeRatio = round(($money - $moneyChainRelative) / $moneyChainRelative * 100, 2);
        }

        return $chainRelativeRatio;
    }

    // 经营数据趋势图
    public function curve($selectParams)
    {
        $returnData = [];

        if ($selectParams['bangdouStoreIds'][0] == '6666'){
            return $returnData;
        }
        // 目前没有赠品支付方式直接返回默认数据
        if ($selectParams['payType'] == 3) {
            return $returnData;
        }

        $objMCurveV2 = new MCurveV2();
        $data        = $objMCurveV2->income($selectParams);
        return $data;
    }

    // 经营数据分析
    public function marketing($selectParams)
    {
        $returnData = [
            [
                'name'               => '会员消费',
                'money'              => 0,
                'chainRelativeRatio' => 0,
                'positive'           => 0,
            ],
            [
                'name'               => '非会员消费',
                'money'              => 0,
                'chainRelativeRatio' => 0,
                'positive'           => 0,
            ],
            [
                'name'               => '邦豆券赠送',
                'money'              => 0,
                'chainRelativeRatio' => 0,
                'positive'           => 0,
            ],
            [
                'name'               => '邦豆券消费',
                'money'              => 0,
                'chainRelativeRatio' => 0,
                'positive'           => 0,
            ],
            [
                'name'               => '现金券赠送',
                'money'              => 0,
                'chainRelativeRatio' => 0,
                'positive'           => 0,
            ],
            [
                'name'               => '现金券消费',
                'money'              => 0,
                'chainRelativeRatio' => 0,
                'positive'           => 0,
            ],
            [
                'name'               => '礼金券赠送',
                'money'              => 0,
                'chainRelativeRatio' => 0,
                'positive'           => 0,
            ],
            [
                'name'               => '礼金券消费',
                'money'              => 0,
                'chainRelativeRatio' => 0,
                'positive'           => 0,
            ],
            [
                'name'               => '新用户赠送现金券',
                'money'              => 0,
                'chainRelativeRatio' => 0,
                'positive'           => 0,
            ],
            [
                'name'               => '现金充值',
                'money'              => 0,
                'chainRelativeRatio' => 0,
                'positive'           => 0,
            ],
            [
                'name'               => '现金充值消费',
                'money'              => 0,
                'chainRelativeRatio' => 0,
                'positive'           => 0,
            ],
            [
                'name'               => '商业充值',
                'money'              => 0,
                'chainRelativeRatio' => 0,
                'positive'           => 0,
            ],
            [
                'name'               => '商业充值消费',
                'money'              => 0,
                'chainRelativeRatio' => 0,
                'positive'           => 0,
            ],
            [
                'name'               => '赠品赠送',
                'money'              => 0,
                'chainRelativeRatio' => 0,
                'positive'           => 0,
            ],
            [
                'name'               => '赠品消费',
                'money'              => 0,
                'chainRelativeRatio' => 0,
                'positive'           => 0,
            ]
        ];

        if ($selectParams['bangdouStoreIds'][0] == '6666'){
            return $returnData;
        }
        // 目前没有赠品支付方式直接返回默认数据 没有充值数据
        if ($selectParams['payType'] == 3) {
            return $returnData;
        }

        // 本期
        $nowData = self::marketingData($selectParams, $returnData);

        // 计算环比开始结束时间,并切换统计时间
        $selectParams              = parent::getChainRelativeTime($selectParams);
        $selectParams['startTime'] = $selectParams['chainRelativeStartTime'];
        $selectParams['endTime']   = $selectParams['chainRelativeEndTime'];
        // 上期
        $lastData = self::marketingData($selectParams, $returnData);

        foreach ($nowData as $key => &$value) {
            $value['chainRelative']      = $lastData[$key]['money'];
            $value['chainRelativeRatio'] = self::chainRelativeRatio($nowData[$key]['money'], $lastData[$key]['money']);
            $value['positive']           = ($value['chainRelativeRatio'] > 0) ? 1 : 0;
        }

        return $nowData;
    }

    // 经营数据统一处理方法
    public function marketingData($selectParams, $returnData)
    {
        $orderPayData = $this->objMRetailOrderV2->orderPayDetail($selectParams);
        /*if (empty($orderPayData)) {
            return $returnData;
        }*/

        switch ($selectParams['incomeType']) {
            case 1:
                $field = 'total';
                break;
            case 2:
                $field = 'discount_price';
                break;
            case 3:
                $field = 'realIncome';
                break;
        }


        foreach ($orderPayData as $key => $value) {
            // 会员消费
            $returnData[0]['money'] += $value[$field];

            // 三券消费
            switch ($value['payType']) {
                case 10:
                    $returnData[12]['money'] += $value[$field];
                    break;
                case 7:
                    $returnData[3]['money'] += $value[$field];
                    break;
                case 6:
                    $returnData[5]['money'] += helper::bcmul($value[$field],0.5);
                    break;
                case 5:
                    $returnData[7]['money'] += $value[$field];
                    break;
            }
        }
        /* return $returnData;*/

        // 邦豆券赠送数据
        $bangdouData            = $this->objMRetailOrderV2->bangdouData($selectParams, 'give_bd');
        $returnData[2]['money'] = helper::bcsub($bangdouData['sendBangdou'], $bangdouData['sendBangdouCancel'], 2);
        // 现金券赠送数据
        $bangdouData            = $this->objMRetailOrderV2->bangdouData($selectParams, 'give_coupon');
        $returnData[4]['money'] = helper::bcsub($bangdouData['sendBangdou'], $bangdouData['sendBangdouCancel'], 2);

        // 礼金券赠送数据
        $bangdouData            = $this->objMRetailOrderV2->bangdouData($selectParams, 'give_gift_coupon');
        $returnData[6]['money'] = helper::bcsub($bangdouData['sendBangdou'], $bangdouData['sendBangdouCancel'], 2);

        // 新用户注册赠送现金券
        $bangdouData            = $this->objMRetailOrderV2->regActivity($selectParams);
        $returnData[8]['money'] = helper::bcsub($bangdouData['sendBangdou'], $bangdouData['sendBangdouCancel'], 2);

        // 商业充值
        $bangdouData            = $this->objMRetailOrderV2->commercialRecharge($selectParams);
        $returnData[11]['money'] = helper::bcsub($bangdouData['rechargeAmount'], $bangdouData['rechargeAmountCancel'], 2);
        return $returnData;
    }


    // 营收数据分析
    public function income($selectParams)
    {
        $returnData = [
            'total'         => [
                'money'              => 0,
                'chainRelative'      => 0,
                'chainRelativeRatio' => 0,
                'positive'           => 0,
            ],
            'cashIncome'    => [
                'cashsale'         => [
                    [
                        'name'  => '现金支付',
                        'money' => 0,
                    ],
                    [
                        'name'  => '微信支付',
                        'money' => 0,
                    ],
                    [
                        'name'  => '通联支付',
                        'money' => 0,
                    ]
                ],
                'cashRecharge'     => [
                    [
                        'name'  => '现金充值支付',
                        'money' => 0,
                    ],
                    [
                        'name'  => '配现金支付',
                        'money' => 0,
                    ],
                ],
                'businessRecharge' => [
                    [
                        'name'  => '商业充值支付',
                        'money' => 0,
                    ],
                    [
                        'name'  => '配现金支付',
                        'money' => 0,
                    ],
                ],
                'onlineShopping'   => [
                ],
            ],
            'bangdouIncome' => [
                'bangdou'        => [
                    [
                        'name'  => '邦豆券支付',
                        'money' => 0,
                    ],
                    [
                        'name'  => '配现金支付',
                        'money' => 0,
                    ],
                ],
                'cashCoupon'     => [
                    [
                        'name'  => '现金券支付',
                        'money' => 0,
                    ],
                    [
                        'name'  => '配现金支付',
                        'money' => 0,
                    ],
                ],
                'cashGiftCoupon' => [
                    [
                        'name'  => '礼金券支付',
                        'money' => 0,
                    ],
                    [
                        'name'  => '配现金支付',
                        'money' => 0,
                    ],
                ],
            ],
            'gift'          => [
                'money' => 0,
            ]
        ];

        if ($selectParams['bangdouStoreIds'][0] == '6666'){
            return $returnData;
        }

        // 目前没有赠品支付方式直接返回默认数据
        if ($selectParams['payType'] == 3) {
            return $returnData;
        }

        $orderPayData = $this->objMRetailOrderV2->orderPayMethonDetail($selectParams);

        if (empty($orderPayData)) {
            return $returnData;
        }

        switch ($selectParams['incomeType']) {
            case 1:
                $field = 'total';
                break;
            case 2:
                $field = 'discount_price';
                break;
            case 3:
                $field = 'realIncome';
                break;
        }

        $nowtotal    = 0;
        $orderIds    = [];
        $orderMoneys = [];

        foreach ($orderPayData as $key => $value) {
            // 判断是不是组合支付订单
            if (!in_array($value['order_id'], $orderIds)) {
                switch ($value['payType']) {
                    case 7:
                        $returnData['bangdouIncome']['bangdou'][0]['money'] += $value[$field];
                        $returnData['bangdouIncome']['bangdou'][1]['money'] += $value['cash'];
                        break;
                    case 6:
                        if($selectParams['incomeType'] !=3){
                            $returnData['bangdouIncome']['cashCoupon'][0]['money'] += $value['deduction_value'];
                        }
                        $returnData['bangdouIncome']['cashCoupon'][1]['money'] += $value['cash'];
                        break;
                    case 5:
                        $returnData['bangdouIncome']['cashGiftCoupon'][0]['money'] += $value['deduction_value'];
                        $returnData['bangdouIncome']['cashGiftCoupon'][1]['money'] += $value['cash'];
                        break;
                    case 20:
                        $returnData['cashIncome']['cashsale'][1]['money'] += $value[$field];
                        break;
                    case 10:
                        $returnData['cashIncome']['businessRecharge'][0]['money'] += $value[$field];
                        $returnData['cashIncome']['businessRecharge'][1]['money'] += $value['cash'];
                        break;

                }
            } else {
                switch ($value['payType']) {
                    case 1:
                        switch ($orderMoneys[$value['order_id']][0]['type']) {
                            case 7:
                                $returnData['bangdouIncome']['bangdou'][1]['money'] += $value[$field];
                                break;
                            case 6:
                                $returnData['bangdouIncome']['cashCoupon'][1]['money'] += $value[$field];
                                break;
                            case 5:
                                $returnData['bangdouIncome']['cashGiftCoupon'][1]['money'] += $value[$field];
                                break;
                            case 10:
                                $returnData['cashIncome']['businessRecharge'][1]['money'] += $value[$field];
                                break;
                        }
                        break;
                }
            }

            $nowtotal                          += $value[$field] + $value['cash'];
            $orderIds[]                        = $value['order_id'];
            //$orderMoneys[$value['order_id']][] = ['type' => $value['payType'], 'money' => $value[$field]];
            //var_dump($orderMoneys[$value['order_id']]);exit;
        }

        $returnData['total']['money'] = $nowtotal;

        // 计算环比开始结束时间,并切换统计时间
        $selectParams              = parent::getChainRelativeTime($selectParams);
        $selectParams['startTime'] = $selectParams['chainRelativeStartTime'];
        $selectParams['endTime']   = $selectParams['chainRelativeEndTime'];

        // 上期
        $orderPayData = $this->objMRetailOrderV2->orderPayMethonDetail($selectParams);
        $lastTotal    = 0;
        if (!empty($orderPayData)) {
            foreach ($orderPayData as $key => $value) {
                $lastTotal += $value[$field];
            }
        }

        $returnData['total']['chainRelative']      = $lastTotal;
        $returnData['total']['chainRelativeRatio'] = self::chainRelativeRatio($nowtotal, $lastTotal);
        $returnData['total']['positive']           = ($returnData['total']['chainRelativeRatio'] > 0) ? 1 : 0;

        return $returnData;
    }

    // 促销数据分析
    public function promotion($selectParams)
    {
        $returnData = [
            'total'    => [
                'money'              => 0,
                'chainRelative'      => 0,
                'chainRelativeRatio' => 0,
                'positive'           => 0,
            ],
            'cashsale' => [
                [
                    'name'  => '正价销售商品',
                    'money' => 0,
                ],
                [
                    'name'  => '折扣销售商品',
                    'money' => 0,
                ],
            ],
            'coupon'   => [
                [
                    'name'  => '正价销售商品',
                    'money' => 0,
                ],
                [
                    'name'  => '折扣销售商品',
                    'money' => 0,
                ],
            ],
            'gift'     => [
                [
                    'name'  => '正价销售商品',
                    'money' => 0,
                ],
                [
                    'name'  => '折扣销售商品',
                    'money' => 0,
                ],
            ]
        ];

        if ($selectParams['bangdouStoreIds'][0] == '6666'){
            return $returnData;
        }
        // 目前没有赠品支付方式直接返回默认数据
        if ($selectParams['payType'] == 3 || $selectParams['payType'] == 4) {
            return $returnData;
        }

        $orderPayData = $this->objMRetailOrderV2->orderPayMethonDetail($selectParams);

        if (empty($orderPayData)) {
            return $returnData;
        }

        switch ($selectParams['incomeType']) {
            case 1:
                $field = 'total';
                break;
            case 2:
                $field = 'discount_price';
                break;
            case 3:
                $field = 'realIncome';
                break;
        }


        $nowtotal = 0;
        foreach ($orderPayData as $key => $value) {
            // 现金消费
            if ($value['payType'] == 20) {//微信支付

                if ($value['discount_price'] == $value['total']) {
                    $returnData['cashsale'][0]['money'] += $value[$field];
                } else {
                    $returnData['cashsale'][1]['money'] += $value[$field];
                }
            }

            // 三券消费
            if (in_array($value['payType'], [7, 6, 5])) {
                if ($value['discount_price'] == $value['total']) {
                    $returnData['coupon'][0]['money'] += $value[$field];
                } else {
                    $returnData['coupon'][1]['money'] += $value[$field];
                }
            }

            $nowtotal += $value[$field];
        }


        $returnData['total']['money'] = $nowtotal;

        // 计算环比开始结束时间,并切换统计时间
        $selectParams              = parent::getChainRelativeTime($selectParams);
        $selectParams['startTime'] = $selectParams['chainRelativeStartTime'];
        $selectParams['endTime']   = $selectParams['chainRelativeEndTime'];

        // 上期
        $orderPayData = $this->objMRetailOrderV2->orderPayMethonDetail($selectParams);
        $lastTotal    = 0;
        if (!empty($orderPayData)) {
            foreach ($orderPayData as $key => $value) {
                $lastTotal += $value[$field];
            }
        }

        $returnData['total']['chainRelative']      = $lastTotal;
        $returnData['total']['chainRelativeRatio'] = self::chainRelativeRatio($nowtotal, $lastTotal);
        $returnData['total']['positive']           = ($returnData['total']['chainRelativeRatio'] > 0) ? 1 : 0;

        return $returnData;
    }


    // 营收概况-实际营收扣减折损方法(基于业绩营收做扣减)
    /* public function realIncomeDeduct($money, $selectParams)
     {
         // 对三券部分做折损操作
         if( in_array($selectParams['payType'], [0,2]) ) {
             $objMBangdouOrder = new MBangdouOrder();
             $data = $objMBangdouOrder->onlinePayForMethod($selectParams);
             if(!$data->isSuccess()){
                 return ResultWrapper::fail($data->getData(), $data->getErrorCode());
             }
             $data = $data->getData();
             $bangdouAppPayData = array_column($data,null,'payType');

             if(!empty($data)){
                 // 减去一半的帮豆
                 $money = bcsub($money, $bangdouAppPayData[7]['realIncome'], 2);

                 // 减去现金券用券部分
                 $money = bcsub($money, $bangdouAppPayData[6]['amount'], 2);

                 // 减去礼金券用券部分
                 $money = bcsub($money, $bangdouAppPayData[5]['amount'], 2);
             }
         }

         // 对现金部分做折损操作
         if( in_array($selectParams['payType'], [0,1]) ){
             $objMRetailOorder = new MRetailOorder();
             $selectParams['storesIds'] = $selectParams['erpStoresId'];
             $resultData  = $objMRetailOorder->offlinePayForMethod($selectParams);
             if(!$resultData->isSuccess()){
                 return ResultWrapper::fail($resultData->getData(), $resultData->getErrorCode());
             }
             $resultData = $resultData->getData();
             if(!empty($resultData)){
                 $giffCardDeductMoeny = 0;
                 foreach($resultData as $key => $value){
                     // 扣掉三分之一
                     if($value['pay_method'] == 2300)
                     {
                         $money = bcsub($money, bcmul(bcdiv($value['pay_money'], 3,2),1,2), 2);
                     }
                 }
             }
         }

         return ResultWrapper::success($money);
     }*/


    // 折损数据分析
    public function depreciation($selectParams)
    {
        $returnData = [
            'total'    => [
                'money'              => 0,
                'chainRelative'      => 0,
                'chainRelativeRatio' => 0,
                'positive'           => 0,
            ],
            'cashsale' => [
                [
                    'name'  => '促销特价折损',
                    'money' => 0,
                ],
                [
                    'name'  => '现金消费邦豆赠送折损',
                    'money' => 0,
                ],
                [
                    'name'  => '现金消费赠品赠送折损',
                    'money' => 0,
                ],
            ],
            'recharge' => [
                [
                    'name'  => '促销特价折损',
                    'money' => 0,
                ],
                [
                    'name'  => '线上充值邦豆赠送消费折损',
                    'money' => 0,
                ],
                [
                    'name'  => '线下充值赠送消费折损',
                    'money' => 0,
                ],
                [
                    'name'  => '商业充值邦豆赠送折损',
                    'money' => 0,
                ],
            ],
            'coupon'   => [
                [
                    'name'  => '促销特价折损',
                    'money' => 0,
                ],
                [
                    'name'  => '邦豆劵消费结算折损',
                    'money' => 0,
                ],
                [
                    'name'  => '现金劵消费结算折损',
                    'money' => 0,
                ],
                [
                    'name'  => '礼金劵消费结算折损',
                    'money' => 0,
                ],
            ],
            'gift'     => [
                [
                    'name'  => '促销特价折损',
                    'money' => 0,
                ],
                [
                    'name'  => '标价赠品赠送折损',
                    'money' => 0,
                ]
            ]
        ];

        if ($selectParams['bangdouStoreIds'][0] == '6666'){
            return $returnData;
        }

        // 目前没有赠品支付方式直接返回默认数据 没有会员充值 标价应收没有折损
        if ($selectParams['payType'] == 3 || $selectParams['incomeType'] == 1) {
            return $returnData;
        }

        $orderPayData = $this->objMRetailOrderV2->orderPayMethonDetail($selectParams);

        if (empty($orderPayData)) {
            return $returnData;
        }

        $returnData = self::commonDeduct($selectParams, $orderPayData, $returnData);

        $nowtotal = 0;
        foreach ($returnData as $key => $value) {
            if ($key == 'total') continue;
            foreach ($value as $k => $v) {
                $nowtotal += $v['money'];
            }
        }

        $returnData['total']['money'] = $nowtotal;


        // 计算环比开始结束时间,并切换统计时间
        $selectParams              = parent::getChainRelativeTime($selectParams);
        $selectParams['startTime'] = $selectParams['chainRelativeStartTime'];
        $selectParams['endTime']   = $selectParams['chainRelativeEndTime'];

        // 上期
        $orderPayData = $this->objMRetailOrderV2->orderPayMethonDetail($selectParams);

        $lastReturnData = self::commonDeduct($selectParams, $orderPayData, $returnData);
        $lastTotal      = 0;
        foreach ($lastReturnData as $key => $value) {
            if ($key == 'total') continue;
            foreach ($value as $k => $v) {
                $lastTotal += $v['money'];
            }
        }

        $returnData['total']['chainRelative']      = $lastTotal;
        $returnData['total']['chainRelativeRatio'] = self::chainRelativeRatio($nowtotal, $lastTotal);
        $returnData['total']['positive']           = ($returnData['total']['chainRelativeRatio'] > 0) ? 1 : 0;

        return $returnData;
    }

    public function commonDeduct($selectParams, $orderPayData, $returnData)
    {
        foreach ($orderPayData as $key => $value) {
            // 现金消费
            if ($value['payType'] == 20) {
                $returnData['cashsale'][0]['money'] += bcsub($value['total'], $value['discount_price'], 2); // 促销折损
            }
            /*if ($value['payType'] == 10) {
                $returnData['recharge'][0]['money'] += bcsub($value['total'], $value['discount_price'], 2); // 促销折损
            }*/
            // 三券消费
            if (in_array($value['payType'], [7, 6, 5])) {
                $returnData['coupon'][0]['money'] += bcsub($value['total'], $value['discount_price'], 2); // 促销折损
            }

            // 实际营收三券折损
            if ($selectParams['incomeType'] == 3) {
                switch ($value['payType']) {
                    case 7:
                        $returnData['coupon'][1]['money'] += bcsub($value['discount_price'], $value['realIncome'], 2);
                        break;
                    case 6:
                        $returnData['coupon'][2]['money'] += bcsub($value['discount_price'], $value['realIncome'], 2);
                        break;
                    case 5:
                        $returnData['coupon'][3]['money'] += bcsub($value['discount_price'], $value['realIncome'], 2);
                        break;
                }

            }
        }

        // 实际营收
        if ($selectParams['incomeType'] == 3) {
            $sendBangdouTotal                   = $this->objMRetailOrderV2->bangdouRewardMoney($selectParams); // 消费赠送邦豆总金额
            $returnData['cashsale'][1]['money'] = helper::bcmul($sendBangdouTotal, 0.5, 2);
        }

        return $returnData;
    }

    // 排行榜
    public function rank($selectParams)
    {
        if ($selectParams['bangdouStoreIds'][0] == '6666'){
            $returnData = [
                'total' => 0,
                'data'  => [],
            ];
            return $returnData;
        }
        return $this->objMRetailOrderV2->orderGoodsDetail($selectParams);
    }

    /**
     * 公共的根据筛选时间对应的方式转化成时间戳
     */
    static function getUnixTimeByDataType($selectParams)
    {
        switch ($selectParams['dateType']) {
            case 0: // 今日
                $unixTime = self::gettimestamp('today');
                break;
            case 1: // 昨日
                $unixTime = self::gettimestamp('yesterday');
                break;
            case 2: // 近七天
                $unixTime = self::gettimestamp('lastSevenDay');
                break;
            case 3: // 本周
                $unixTime = self::gettimestamp('beforweek');
                break;
            case 4: // 上周
                $unixTime = self::gettimestamp('preweek');
                break;
            case 5: // 本月
                $unixTime = self::gettimestamp('nowmonth');
                break;
            case 6: // 上月
                $unixTime = self::gettimestamp('permonth');
                break;
            case 7: // 本季度
                $unixTime = self::gettimestamp('quarter');
                break;
            case 8: // 上季度
                $unixTime = self::gettimestamp('lastQuarter');
                break;
            case 9: // 本年
                $unixTime = self::gettimestamp('nowyear');
                break;
            case 10: // 去年
                $unixTime = self::gettimestamp('preyear');
                break;
            case 11: // 自定义
                $unixTime['start'] = strtotime($selectParams['startTime'] . ' 0:0:0');
                $unixTime['end']   = strtotime($selectParams['endTime'] . ' 23:59:59');
                break;
        }

        $selectParams['startTime'] = $unixTime['start'];
        $selectParams['endTime']   = $unixTime['end'];
        return $selectParams;
    }

    /**
     * 分页方法
     */
    function pageToOffset($page = 1, $pageSize = 10)
    {
        return [
            'limit'  => $pageSize,
            'offset' => ($page - 1) * $pageSize,
        ];
    }
}
